home *** CD-ROM | disk | FTP | other *** search
- //////////////////////////////////////////////////////////////////////////////
- // Routines for load/save RLE compressed sprite files
- // Copyright 1997 Aleksei Sujurov
- //////////////////////////////////////////////////////////////////////////////
-
- // I N C L U D E /////////////////////////////////////////////////////////////
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <conio.h>
- #include <wgt5.h>
-
- #include "rle_spr.h"
-
- // D E F I N E S ///////////////////////////////////////////////////////////
-
- #define NORMAL 0
- #define RLE 1
- #define MAX_SPR 2000
-
- // P R O T O T Y P E S ///////////////////////////////////////////////////////
-
- static int EncodeRun(int val, int run_length, FILE * file);
- static int EncodeSprite (FILE * file,
- block line_buffer,
- unsigned int length);
- static int DecodeSprite (FILE* file, block line_buffer, unsigned int length);
-
- // F U N C T I O N S ////////////////////////////////////////////////////////
-
- short wloadsprites_rle (color *pal, char *filename, block *image_array,
- short start, short end)
- {
- short i, a;
- short maxspr;
- short made;
- short width, height;
- char buf[20];
- long size;
-
- if (wgtlibrary == NULL)
- {
- if ((libf = fopen (filename, "rb")) == NULL)
- {
- return (-1);
- }
- }
- else
- {
- if ((libf = fopen (wgtlibrary, "rb")) == NULL)
- {
- return (-1);
- }
- readheader ();
- findfile (filename);
- if (lresult == 1)
- fseek (libf, lfpos, SEEK_SET);
- if (checkpassword (password) == 0)
- {
- wsetmode (3);
- fprintf (stderr, "Incorrect password\n");
- exit (-1);
- }
- }
-
- fread (&a, 2, 1, libf);
- if (a!=1)
- {
- return (-1);
- }
-
- fread (buf, 17, 1, libf);
- if (strnicmp (" RLE Sprite File ", buf, 17)!=0)
- {
- return (-1);
- }
-
- fread (pal, 768, 1, libf);
- fread (&maxspr, 2, 1, libf);
-
- if (maxspr>MAX_SPR)
- maxspr=MAX_SPR;
-
- if (end>maxspr)
- end=maxspr;
-
- if (start<0)
- start=0;
-
- for (i=0; i<=end; i++)
- {
-
- fread (&made, 2, 1, libf);
-
- if (made==1)
- {
- fread (&width, 2, 1, libf);
- fread (&height, 2, 1, libf);
- fread (&size, 4, 1, libf);
-
- if (i>=start)
- {
- if ((image_array[i]=(block) malloc (width*height+4))==NULL)
- {
- return (-1);
- }
- fseek (libf, -8, SEEK_CUR);
- fread (image_array[i], 4, 1, libf);
- fseek (libf, 4, SEEK_CUR);
-
- if (DecodeSprite (libf, image_array[i]+4, width*height))
- {
- fclose (libf);
- return (-1);
-
- }
- }
- else
- fseek (libf, size, SEEK_CUR);
- }
-
- if (i>=start)
- if (!made)
- image_array[i]=NULL;
-
- }
-
- fclose (libf);
-
- return (0);
- }
-
- //////////////////////////////////////////////////////////////////////////////
-
- short wsavesprites_rle (color *pal, char *filename, block *image_array,
- short start, short end)
- {
-
- FILE* fp;
- short i;
-
- short version=0x01;
- char name[]=" RLE Sprite File ";
- short maxspr=MAX_SPR;
-
- short made;
- short width, height;
- fpos_t fpos1, fpos2;
- long size;
-
-
- if ((fp=fopen (filename, "wb"))==NULL)
- {
- return (-1);
- }
-
- fwrite (&version, sizeof (short), 1, fp);
- fputs (name, fp);
- fwrite (pal, 768, 1, fp);
-
- for (i=end; i>=start; i--)
- {
- if (image_array[i]!=NULL){
- maxspr=i;
- break;
- }
- }
-
- if (maxspr>MAX_SPR)
- maxspr=MAX_SPR;
-
- if (end>maxspr)
- end=maxspr;
-
- if (start<0)
- start=0;
-
- fwrite (&end, sizeof (short), 1, fp);
-
- for (i=start; i<=end; i++)
- {
-
- if (image_array[i]!=NULL)
- made=1;
- else
- made=0;
-
- fwrite (&made, sizeof (short), 1, fp);
-
- if (made)
- {
- width=wgetblockwidth (image_array[i]);
- height=wgetblockheight (image_array[i]);
-
- fwrite (&width, sizeof (short), 1, fp);
- fwrite (&height, sizeof (short), 1, fp);
-
- fgetpos (fp, &fpos1);
- fseek (fp, sizeof (long), SEEK_CUR);
-
- if (EncodeSprite (fp, image_array[i]+4, width*height))
- {
- fclose (fp);
- return (-1);
- }
-
- fgetpos (fp, &fpos2);
- size=fpos2-fpos1;
- fsetpos (fp, &fpos1);
- fwrite (&size, sizeof (long), 1, fp);
- fsetpos (fp, &fpos2);
-
- }
-
- }
-
- fclose (fp);
- return (0);
-
- }
-
- //////////////////////////////////////////////////////////////////////////////
-
- static int EncodeRun(int val, int run_length, FILE * file)
- {
- if (run_length)
- {
- if ((run_length == 1) && (0xC0 != (0xC0 & val)))
- {
- if (EOF == fputc(val, file))
- return (-1);
- }
- else
- {
- if (EOF == fputc(0xC0 | run_length, file))
- return (-1);
-
- if (EOF == fputc(val, file))
- return (-1);
- }
- }
-
- return (0);
-
- }
-
- //////////////////////////////////////////////////////////////////////////////
-
- static int EncodeSprite (FILE * file,
- block line_buffer,
- unsigned int length)
- {
- int oldval;
- int newval;
- int count;
- unsigned length_counter;
-
- oldval = *line_buffer++;
- length_counter = 1;
- count = 1;
-
- while (length_counter < length)
- {
- newval = *line_buffer++;
-
- if (newval == oldval)
- {
- count++;
- if (count == 63)
- {
- if (EncodeRun(oldval, count, file))
- return (-1);
- count = 0;
- }
- }
- else
- {
- if (count)
- if (EncodeRun(oldval, count, file))
- return (-1);
-
- oldval = newval;
- count = 1;
- }
-
- length_counter++;
-
- }
-
- if (count)
- if (EncodeRun(oldval, count, file))
- return (-1);
-
- return (0);
- }
-
- //////////////////////////////////////////////////////////////////////////////
-
- static int DecodeSprite (FILE* file, block line_buffer, unsigned int length)
- {
- unsigned int i;
- short mode=NORMAL;
- short nbytes;
- char abyte;
-
- for (i=0; i<length; i++)
- {
- if (mode == NORMAL)
- {
- if ((abyte=fgetc(file))==EOF)
- return (-1);
- if ((unsigned char)abyte > 0xbf)
- {
- nbytes=abyte & 0x3f;
- if ((abyte=fgetc(file))==EOF)
- return (-1);
- if (--nbytes > 0)
- mode=RLE;
- }
- }
- else
- if (--nbytes == 0)
- mode=NORMAL;
-
- *line_buffer++=abyte;
- }
-
- return (0);
-
- }
-